/*
 * GwtYuiCarousel - API to integrate YUI Carousel in GWT framework
 *
 * Copyright (c) 2007, Nicolas Hoppenot
 *
 * This free software is licensed under the Apache License, Version 2.0 (the "License").
 * To obtain a copy of the licence, visit : http://www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed WITHOUT ANY WARRANTY OR CONDITION OF ANY KIND.
 * Please see the license for more information.
 *
 * Version $0.6.1-beta1$
 */
package com.gwtyuicarousel.client.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.ComplexPanel;

/**
 * Create the HTML elements required by the carousel (for more information, please see
 * http://billwscott.com/carousel/#markup) : <code>
 * <div id="carouselId" class="carousel-component">
 *   <div class="carousel-clip-region">
 *     <ul class="carousel-list">
 *     </ul>
 *   </div>
 * </div>
 * </code>
 * 
 * After the HTML elements have been attached to the document, the Carousel is instanciated.
 * 
 * @version $0.6.1-beta1$
 * @author Nicolas Hoppenot
 * 
 */
public class RequiredCarouselStructure
    extends ComplexPanel {

  /** Contains the initial configuration of the carousel */
  protected CarouselConfig config;

  /**
   * Create the carousel structure, and instanciate the carousel with the specified configuiration.
   * 
   * @param carouselId
   *          The ID of the root HTML element of the carousel (of the DIV element of class
   *          carousel-component)
   * @param config
   *          The configuration of the carousel
   */
  public RequiredCarouselStructure(String carouselId, CarouselConfig config) {
    setElement(createBasicElements(carouselId));
    this.config = config;
  }

  /**
   * Create the carousel structure, and instanciate the carousel with the specified configuiration.
   * 
   * @param carouselId
   *          The ID of the root HTML element of the carousel (of the DIV element of class
   *          carousel-component)
   */
  public RequiredCarouselStructure(String carouselId) {
    setElement(createBasicElements(carouselId));
  }

  /**
   * Create the required carousel structure :<br>
   * <ul>
   * <li>The DIV element of the specified ID, and of class "carousel-component"</li>
   * <li>Call {@link #createClipRegion(Element)} to create the content list HTML structure</li>
   * </ul>
   * 
   * @param carouselId
   *          The ID of the root element of the carousel
   * @return The root element of the carousel (element of the given ID)
   */
  private final Element createBasicElements(String carouselId) {
    Element carouselElement = DOM.createDiv();
    DOM.setElementAttribute(carouselElement, "id", carouselId);
    DOM.setElementProperty(carouselElement, "className", "carousel-component");
    createClipRegion(carouselElement);
    return carouselElement;
  }

  /**
   * Create the requierd DIV structure for the contents of the carousel : <br>
   * <ul>
   * <li>The DIV element of class "carousel-clip-region"</li>
   * <li>The UL element of class "carousel-list"</li>
   * </ul>
   * 
   * @param carouselElement
   *          The HTML root element of the carousel
   */
  private final void createClipRegion(Element carouselElement) {
    Element clipRegion = DOM.createDiv();
    DOM.setElementProperty(clipRegion, "className", "carousel-clip-region");
    DOM.appendChild(carouselElement, clipRegion);
    Element list = DOM.createElement("ul");
    DOM.setElementProperty(list, "className", "carousel-list");
    DOM.appendChild(clipRegion, list);
  }

  /**
   * We wait that the carousel DIV root element has been attached to the browser's document to
   * instanciate the carousel.
   * 
   * @see com.google.gwt.user.client.ui.Widget#onLoad()
   */
  protected void onLoad() {
    createCarousel();
  }

  /**
   * Define the appropriate subclass of carousel to instanciate
   */
  protected void createCarousel() {
    new Carousel(DOM.getElementAttribute(getElement(), "id"), config);
  }

}
